package com.bcxj.routeplan.client;

import com.bcxj.internalcommon.constant.AmapConfigConstants;
import com.bcxj.internalcommon.constant.CommonStatusEnum;
import com.bcxj.internalcommon.response.ResponseResult;
import com.bcxj.internalcommon.util.RedisPrefixUtils;
import com.bcxj.internalcommon.vo.*;
import com.bcxj.routeplan.utils.RecommendUtil;
import com.bcxj.routeplan.utils.RedisCacheUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * ClassName: RoutePlanClient
 * Package: com.bcxj.routeplan.client
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/13 09:46
 * @Version 1.0
 * @Since 1.0
 */
@Slf4j
@Component
public class RoutePlanClient {

    @Value("${amap.key}")
    private String key;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisCacheUtils redisCacheUtils;

    // 步行
    //https://restapi.amap.com/v3/direction/walking?origin=116.434307,39.90909&destination=116.434446,39.90816&key=556b1991b6ec5f24d148342a93b20e79
    public ResponseResult getDirectionWalk(String depLongitude, String depLatitude, String destLongitude, String destLatitude) {

        // 数据写入redis中
        String jsonStr = redisCacheUtils.queryWithLogicalExpire(RedisPrefixUtils.WALK_PREFIX,
                depLatitude + "-" + depLongitude + "-" + destLatitude + "-" + destLongitude,
                String.class, 120L, TimeUnit.MINUTES,
                "path:walk", 5L, TimeUnit.MINUTES,
                10L, TimeUnit.MINUTES,
                (String s) -> {
//                    ResponseResult walkByStr = getWalkByStr(depLongitude, depLatitude, destLongitude, destLatitude, getWalkListStr(depLongitude, depLatitude, destLongitude, destLatitude));
                    return getWalkListStr(depLongitude, depLatitude, destLongitude, destLatitude);
                });
        return getWalkByStr(depLongitude, depLatitude, destLongitude, destLatitude, jsonStr);
    }

    private String getWalkListStr(String depLongitude, String depLatitude, String destLongitude, String destLatitude) {
        // 1.构建请求
        StringBuilder str = new StringBuilder();
        str.append(AmapConfigConstants.WALK_HTTP);
        str.append("origin=");
        str.append(depLongitude + "," + depLatitude);
        str.append("&destination=");
        str.append(destLongitude + "," + destLatitude);
        str.append("&key=");
        str.append(key);
        log.info("getDirectionWalk url:{}", str);
        // 2. 发送请求
        ResponseEntity<String> entity = restTemplate.getForEntity(str.toString(), String.class);
        System.out.println(entity.getBody());
        String jsonStr = entity.getBody();

        // 虚拟数据
//        String jsonStr = """
//                {"status":"1","info":"ok","infocode":"10000","count":"1","route":{"origin":"106.67034,26.40985","destination":"106.675885,26.449770","paths":[{"distance":"4986","duration":"3989","steps":[{"instruction":"向东步行76米左转","orientation":"东","road":[],"distance":"76","duration":"61","polyline":"106.670299,26.410113;106.670456,26.410135;106.671029,26.410334","action":"左转","assistant_action":[],"walk_type":"0"},{"instruction":"向北步行32米右转","orientation":"北","road":[],"distance":"32","duration":"26","polyline":"106.671029,26.410334;106.671029,26.410369;106.671029,26.410625","action":"右转","assistant_action":[],"walk_type":"0"},{"instruction":"向东步行67米左转","orientation":"东","road":[],"distance":"67","duration":"54","polyline":"106.671024,26.410625;106.671228,26.410629;106.671228,26.410629;106.671658,26.410634;106.671658,26.410634;106.67171,26.410634","action":"左转","assistant_action":[],"walk_type":"0"},{"instruction":"沿田园路向北步行738米右转","orientation":"北","road":"田园路","distance":"738","duration":"590","polyline":"106.67171,26.410634;106.67171,26.41105;106.671753,26.411181;106.671753,26.411181;106.671753,26.411272;106.671753,26.411272;106.671753,26.411684;106.671753,26.411684;106.671766,26.412734;106.671766,26.412734;106.671762,26.413108;106.671762,26.413108;106.671762,26.413177;106.671762,26.413177;106.671762,26.413468;106.67174,26.414136;106.67174,26.414696;106.671714,26.415412;106.67168,26.415747;106.671619,26.416094;106.671528,26.416467;106.671389,26.416875;106.671233,26.417253","action":"右转","assistant_action":[],"walk_type":"0"},{"instruction":"沿香椿路步行622米左转","orientation":[],"road":"香椿路","distance":"622","duration":"498","polyline":"106.671228,26.417253;106.671298,26.417279;106.671298,26.417279;106.671597,26.417378;106.671597,26.417378;106.671771,26.417439;106.671884,26.417461;106.672031,26.417465;106.672422,26.417465;106.672604,26.417465;106.67273,26.417474;106.67273,26.417474;106.672995,26.417535;106.673108,26.417587;106.673286,26.417695;106.673368,26.417752;106.673368,26.417752;106.673832,26.418125;106.674158,26.418325;106.674366,26.418424;106.675369,26.418845;106.675521,26.418937;106.675521,26.418937;106.675616,26.418971;106.675616,26.418971;106.675885,26.419084;106.675885,26.419084;106.676029,26.419128;106.676059,26.419132;106.676102,26.419132;106.676102,26.419132;106.676146,26.419128;106.676259,26.419097;106.676884,26.418854","action":"左转","assistant_action":[],"walk_type":"0"},{"instruction":"沿清溪路辅路向北步行58米直行","orientation":"北","road":"清溪路辅路","distance":"58","duration":"46","polyline":"106.676884,26.41885;106.676962,26.419375","action":"直行","assistant_action":[],"walk_type":"25"},{"instruction":"步行30米直行","orientation":[],"road":[],"distance":"30","duration":"24","polyline":"106.676962,26.419375;106.676975,26.419431;106.677014,26.419648","action":"直行","assistant_action":[],"walk_type":"1"},{"instruction":"沿清溪路辅路向北步行360米右转","orientation":"北","road":"清溪路辅路","distance":"360","duration":"288","polyline":"106.677014,26.419648;106.677122,26.420291;106.677122,26.420291;106.677148,26.420443;106.677148,26.420443;106.677253,26.420985;106.677253,26.420985;106.677331,26.421402;106.677331,26.421402;106.677387,26.421684;106.677387,26.421684;106.677504,26.422274;106.677504,26.422274;106.677556,26.422526;106.677556,26.422526;106.677587,26.422691;106.677674,26.422847;106.677674,26.422847;106.677678,26.422878","action":"右转","assistant_action":[],"walk_type":"0"},{"instruction":"沿学士路步行18米左转","orientation":[],"road":"学士路","distance":"18","duration":"14","polyline":"106.677678,26.422878;106.677865,26.422852","action":"左转","assistant_action":[],"walk_type":"0"},{"instruction":"沿清溪路辅路向北步行172米直行","orientation":"北","road":"清溪路辅路","distance":"172","duration":"138","polyline":"106.677865,26.422847;106.677895,26.423008;106.677895,26.423008;106.678095,26.424076;106.678095,26.424076;106.678138,26.424253;106.678138,26.424253;106.678147,26.424401","action":"直行","assistant_action":[],"walk_type":"0"},{"instruction":"沿清溪路向北步行1113米直行","orientation":"北","road":"清溪路","distance":"1113","duration":"890","polyline":"106.678147,26.424401;106.678155,26.42447;106.678155,26.42447;106.67822,26.424826;106.678242,26.425039;106.678242,26.425039;106.678264,26.4252;106.678277,26.425508;106.678277,26.425508;106.678281,26.425681;106.678281,26.425681;106.678277,26.425972;106.678277,26.425972;106.678268,26.426115;106.678238,26.426389;106.67822,26.426497;106.67822,26.426497;106.678155,26.426927;106.67809,26.427274;106.67809,26.427274;106.678073,26.427361;106.678073,26.427361;106.67806,26.427444;106.67806,26.427444;106.678012,26.427678;106.678012,26.427678;106.677969,26.427912;106.677925,26.428203;106.677925,26.428203;106.677899,26.428442;106.677899,26.428442;106.677882,26.428628;106.677873,26.428841;106.677873,26.428841;106.677873,26.429345;106.677882,26.429497;106.677882,26.429497;106.677886,26.429714;106.677886,26.429918;106.677886,26.429918;106.677886,26.430312;106.677886,26.430312;106.677886,26.43036;106.677886,26.43036;106.677891,26.430464;106.677891,26.430464;106.677895,26.430538;106.677895,26.430538;106.677899,26.430677;106.677899,26.430677;106.677904,26.430755;106.677904,26.430755;106.677934,26.431671;106.677934,26.431671;106.677938,26.43174;106.677938,26.43174;106.677969,26.432361;106.677969,26.432361;106.677969,26.432431;106.677969,26.432431;106.677982,26.432622;106.67799,26.432826;106.67799,26.432826;106.677995,26.432895;106.677995,26.432895;106.677999,26.432964;106.677999,26.432964;106.678008,26.433364;106.677999,26.433546;106.677999,26.433546;106.677977,26.433733;106.677977,26.433733;106.677964,26.433811;106.677912,26.43408;106.67786,26.434253;106.67783,26.43434;106.677756,26.43451","action":"直行","assistant_action":[],"walk_type":"0"},{"instruction":"沿花溪大道南段向北步行1700米到达目的地","orientation":"北","road":"花溪大道南段","distance":"1700","duration":"1360","polyline":"106.677752,26.43451;106.677613,26.434839;106.677548,26.435013;106.6774,26.435425;106.6774,26.435425;106.67717,26.436029;106.67717,26.436029;106.677153,26.436076;106.677153,26.436076;106.677135,26.436115;106.677066,26.436324;106.677066,26.436324;106.677049,26.436402;106.677049,26.436402;106.677036,26.436476;106.677018,26.436636;106.677018,26.436636;106.67701,26.436745;106.67701,26.436745;106.67701,26.436801;106.67701,26.436801;106.67701,26.436931;106.67701,26.436931;106.677023,26.437739;106.677023,26.437739;106.677023,26.437782;106.677023,26.437782;106.677027,26.438194;106.677027,26.438194;106.67704,26.438724;106.67704,26.438724;106.677049,26.439288;106.677049,26.439288;106.677053,26.439566;106.677053,26.439566;106.677053,26.439601;106.677053,26.439601;106.677053,26.439688;106.677053,26.439688;106.677083,26.440352;106.677083,26.440352;106.677101,26.441259;106.677088,26.441762;106.677049,26.442179;106.676962,26.44299;106.676962,26.44299;106.676914,26.44342;106.676914,26.44342;106.676858,26.44388;106.676858,26.44388;106.676806,26.444349;106.676806,26.444349;106.676758,26.444805;106.676641,26.445855;106.676615,26.445994;106.676615,26.445994;106.676519,26.44674;106.676424,26.447387;106.676354,26.447756;106.676354,26.447756;106.676289,26.448173;106.676289,26.448173;106.676233,26.448446;106.676233,26.448446;106.675951,26.449779","action":[],"assistant_action":"到达目的地","walk_type":"0"}]}]}}
//                """;
        return jsonStr;
    }

    private ResponseResult getWalkByStr(String depLongitude, String depLatitude, String destLongitude, String destLatitude, String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)) {
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_ERROR);
        }
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);
        // 3. 分析数据
        //3.1 检查状态
        if (!jsonObject.has("status") || !jsonObject.getString("status").equals("1")) {
            // 调用方需要自己去做检查
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_ERROR);
        }
        if (!jsonObject.has("route")) {
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_ERROR);
        }

        JSONObject route = jsonObject.getJSONObject("route");
        // 3.2 提取路径信息
        JSONArray paths = route.getJSONArray("paths");
        if (paths.isEmpty()) {
            return ResponseResult.fail("No paths found");
        }

        ArrayList<DirectionWalkVO> directionWalkVOS = new ArrayList<>();
        for (int i = 0; i < paths.size(); i++) {
            JSONObject path = paths.getJSONObject(i);
            String distance = path.getString("distance");
            String duration = path.getString("duration");

            // 3.3 提取分步信息
            JSONArray steps = path.getJSONArray("steps");
            DirectionWalkVO directionWalkVO = new DirectionWalkVO();
            directionWalkVO.setDepLatitude(depLatitude);
            directionWalkVO.setDepLongitude(depLongitude);
            directionWalkVO.setDestLatitude(destLatitude);
            directionWalkVO.setDestLongitude(destLongitude);
            directionWalkVO.setDistance(Double.parseDouble(distance));
            directionWalkVO.setDuration(Double.parseDouble(duration));
            directionWalkVO.setSteps(steps.size());
            directionWalkVO.setWalkSteps(new ArrayList<DirectionWalkVO.WalkStep>());

            for (int j = 0; j < steps.size(); j++) {
                JSONObject step = steps.getJSONObject(j);
                String instruction = step.getString("instruction");
                String orientation = step.getString("orientation");
                String road = step.has("road") ? step.getString("road") : "";
                String stepDistance = step.getString("distance");
                String stepDuration = step.getString("duration");
                String polyline = step.getString("polyline");
                String action = step.getString("action");

                DirectionWalkVO.WalkStep stepVO = new DirectionWalkVO.WalkStep();
                stepVO.setInstruction(instruction);
                stepVO.setOrientation(orientation);
                stepVO.setRoad(road);
                stepVO.setStepDistance(stepDistance);
                stepVO.setStepDuration(stepDuration);
                stepVO.setPolyline(polyline);
                stepVO.setAction(action);

                directionWalkVO.getWalkSteps().add(stepVO);
            }
            directionWalkVOS.add(directionWalkVO);
        }
        return ResponseResult.success(directionWalkVOS);
    }

    ;

    // 获取驾车路径规划
    //https://restapi.amap.com/v5/direction/driving?origin=116.434307,39.90909&destination=116.434446,39.90816&key=<用户的key>
    public ResponseResult getDirectionDrive(String depLongitude, String depLatitude, String destLongitude, String destLatitude, String strategy) {

        // 数据写入redis中
        String jsonStr = redisCacheUtils.queryWithLogicalExpire(RedisPrefixUtils.DRIVE_PREFIX,
                depLatitude + "-" + depLongitude + "-" + destLatitude + "-" + destLongitude + "-" + strategy,
                String.class, 120L, TimeUnit.MINUTES,
                "path:drive", 5L, TimeUnit.MINUTES,
                10L, TimeUnit.MINUTES,
                (String s) -> {
//                    ResponseResult walkByStr = getWalkByStr(depLongitude, depLatitude, destLongitude, destLatitude, getWalkListStr(depLongitude, depLatitude, destLongitude, destLatitude));
                    return getDriveListStr(depLongitude, depLatitude, destLongitude, destLatitude, strategy);
                });
        return getDriveByStr(depLongitude, depLatitude, destLongitude, destLatitude, jsonStr);
    }

    private ResponseResult getDriveByStr(String depLongitude, String depLatitude, String destLongitude, String destLatitude, String jsonStr) {
        if(StringUtils.isEmpty(jsonStr)) {
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_ERROR);
        }

        JSONObject jsonObject = JSONObject.fromObject(jsonStr);


        if (!jsonObject.has("status") || !jsonObject.getString("status").equals("1")) {
            // 调用方需要自己去做检查
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_ERROR);
        }
        if (!jsonObject.has("route")) {
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_ERROR);
        }
        JSONObject route = jsonObject.getJSONObject("route");
        Double taxiCost = Double.parseDouble(route.getString("taxi_cost")); // 获取路径的总距离
        // 3.2 提取路径信息
        JSONArray paths = route.getJSONArray("paths");
        if (paths.isEmpty()) {
            return ResponseResult.fail("No paths found");
        }

        ArrayList<DirectionDriveVO> directionDriveVOS = new ArrayList<>();
        for (int i = 0; i < paths.size(); i++) {
            // 提取路径信息
            JSONObject path = paths.getJSONObject(i);
            Double distance = Double.parseDouble(path.getString("distance")); // 获取路径的总距离

            // 解析每一步的路径信息
            JSONArray steps = path.getJSONArray("steps");
            List<DirectionDriveVO.DriveStep> driveSteps = new ArrayList<>();
            for (int j = 0; j < steps.size(); j++) {
                JSONObject step = steps.getJSONObject(j);
                DirectionDriveVO.DriveStep driveStep = new DirectionDriveVO.DriveStep(
                        step.getString("instruction"),    // 步骤指示
                        step.getString("orientation"),    // 方向
                        step.optString("road_name", ""),      // 路名 (如果没有可以返回空字符串)
                        step.getString("step_distance"), // 步长
                        step.getString("polyline")      // 路径坐标
                );
                driveSteps.add(driveStep);
            }

            DirectionDriveVO directionDriveVO = new DirectionDriveVO();
            directionDriveVO.setDepLongitude(depLongitude);
            directionDriveVO.setDepLatitude(depLatitude);
            directionDriveVO.setDestLongitude(destLongitude);
            directionDriveVO.setDestLatitude(destLatitude);
            directionDriveVO.setDistance(distance);
            directionDriveVO.setTaxiCost(taxiCost);
            directionDriveVO.setWalkSteps(driveSteps);
            directionDriveVOS.add(directionDriveVO);
        }

        return ResponseResult.success(directionDriveVOS);
    }

    private String getDriveListStr(String depLongitude, String depLatitude, String destLongitude, String destLatitude, String strategy) {

        // 驾车的路径的规划
        StringBuilder str = new StringBuilder();
        str.append(AmapConfigConstants.DRIVE_HTTP);
        str.append("origin=");
        str.append(depLongitude + "," + depLatitude);
        str.append("&destination=");
        str.append(destLongitude + "," + destLatitude);
        str.append("&show_fields=polyline");
        if (!StringUtils.isEmpty(strategy)) {
            str.append("&strategy=" + strategy.trim());
        }
        str.append("&key=");
        str.append(key);

        // 发送请求
        ResponseEntity<String> entity = restTemplate.getForEntity(str.toString(), String.class);
        System.out.println(entity.getBody());
        String jsonStr = entity.getBody();

//        // 虚拟数据
//        String jsonStr = """
//                      {"status":"1","info":"OK","infocode":"10000","count":"3","route":{"origin":"106.67034,26.40985","destination":"106.675885,26.449770","taxi_cost":"18","paths":[{"distance":"6472","restriction":"0","steps":[{"instruction":"向西南行驶77米左转","orientation":"西南","step_distance":"77","polyline":"106.67032,26.409636;106.6701,26.409609;106.670014,26.409582;106.669666,26.409308"},{"instruction":"向南行驶66米右转","orientation":"南","step_distance":"66","polyline":"106.669666,26.409308;106.669666,26.409233;106.66966,26.409072;106.669655,26.408917;106.669655,26.408863;106.66966,26.40881;106.66966,26.408761;106.66966,26.408697"},{"instruction":"沿明珠大道向西行驶425米向右前方行驶进入右转专用道","orientation":"西","road_name":"明珠大道","step_distance":"425","polyline":"106.66966,26.408697;106.668738,26.408697;106.668394,26.408713;106.668094,26.408772;106.665599,26.409453;106.665508,26.40948"},{"instruction":"沿花桐北路向西北行驶46米右转","orientation":"西北","road_name":"花桐北路","step_distance":"46","polyline":"106.665508,26.40948;106.665363,26.40962;106.665267,26.409829"},{"instruction":"沿花桐北路向北行驶997米靠左","orientation":"北","road_name":"花桐北路","step_distance":"997","polyline":"106.665267,26.409829;106.665315,26.409995;106.665685,26.411132;106.66591,26.411846;106.665975,26.412098;106.666007,26.412312;106.666028,26.412607;106.666023,26.413101;106.666007,26.414163;106.665991,26.415413;106.665991,26.415504;106.665926,26.418041;106.665937,26.418755"},{"instruction":"沿花桐北路向北行驶245米直行进入隧道","orientation":"北","road_name":"花桐北路","step_distance":"245","polyline":"106.665937,26.418755;106.665873,26.419055;106.665825,26.42096"},{"instruction":"沿花桐北路途径花溪平桥、甲秀南路向北行驶1.4千米向右前方行驶进入辅路","orientation":"北","road_name":"花桐北路","step_distance":"1364","polyline":"106.665825,26.42096;106.665825,26.421094;106.665986,26.422118;106.66613,26.423154;106.666141,26.423529;106.66612,26.423975;106.666071,26.424371;106.665637,26.426201;106.665599,26.426431;106.665589,26.426651;106.665594,26.426877;106.665631,26.42715;106.665873,26.42861;106.666028,26.429484;106.666104,26.429811;106.666554,26.431431;106.666624,26.431544;106.666688,26.431791;106.666769,26.432134;106.666973,26.432896;106.667032,26.433083"},{"instruction":"沿甲秀南路辅路向北行驶275米右转进入右转专用道","orientation":"北","road_name":"甲秀南路辅路","step_distance":"275","polyline":"106.667032,26.433083;106.667042,26.433105;106.667295,26.433652;106.667375,26.433968;106.667429,26.434108;106.66759,26.434773;106.667648,26.435165;106.667675,26.435492"},{"instruction":"沿溪北路向东北行驶21米右转","orientation":"东北","road_name":"溪北路","step_distance":"21","polyline":"106.667675,26.435492;106.66774,26.43554;106.667868,26.435573"},{"instruction":"沿溪北路向东行驶938米左转","orientation":"东","road_name":"溪北路","step_distance":"938","polyline":"106.667868,26.435573;106.667949,26.435546;106.668743,26.435293;106.669091,26.435202;106.669242,26.435176;106.669564,26.435143;106.669987,26.435181;106.670261,26.43524;106.670535,26.435326;106.670717,26.435401;106.670915,26.435513;106.671178,26.435685;106.671506,26.435878;106.671865,26.436018;106.672342,26.43612;106.672707,26.436109;106.67297,26.436077;106.673174,26.436045;106.673877,26.435921;106.674123,26.435894;106.674316,26.435894;106.6751,26.436002;106.676441,26.436205;106.67695,26.436302"},{"instruction":"沿花溪大道南段向北行驶1.6千米向右前方行驶进入辅路","orientation":"北","road_name":"花溪大道南段","step_distance":"1571","polyline":"106.677047,26.436404;106.677015,26.43664;106.67701,26.436747;106.67701,26.436801;106.67701,26.436935;106.677026,26.43774;106.677026,26.437783;106.677031,26.438196;106.677042,26.438727;106.677047,26.43929;106.677052,26.439569;106.677052,26.439601;106.677052,26.439692;106.677085,26.440352;106.677101,26.441259;106.677085,26.441763;106.676956,26.442991;106.676908,26.443426;106.676832,26.444091;106.676613,26.445996;106.676419,26.44739;106.676355,26.44776;106.676285,26.448173;106.676231,26.448447;106.67591,26.449955;106.675803,26.450571"},{"instruction":"沿花溪大道南段辅路向北行驶179米左转调头","orientation":"北","road_name":"花溪大道南段辅路","step_distance":"179","polyline":"106.675803,26.450571;106.675813,26.45114;106.675685,26.452181"},{"instruction":"沿花溪大道南段辅路途径花溪大道南段向南行驶268米到达目的地","orientation":"南","road_name":"花溪大道南段辅路","step_distance":"268","polyline":"106.675346,26.452138;106.67539,26.451773;106.675421,26.451558;106.675561,26.450791;106.675636,26.450566;106.675717,26.45041;106.675825,26.449761"}]},{"distance":"6216","restriction":"0","steps":[{"instruction":"向西南行驶77米左转","orientation":"西南","step_distance":"77","polyline":"106.67032,26.409636;106.6701,26.409609;106.670014,26.409582;106.669666,26.409308"},{"instruction":"向南行驶66米右转","orientation":"南","step_distance":"66","polyline":"106.669666,26.409308;106.669666,26.409233;106.66966,26.409072;106.669655,26.408917;106.669655,26.408863;106.66966,26.40881;106.66966,26.408761;106.66966,26.408697"},{"instruction":"沿明珠大道向西行驶92米左转调头","orientation":"西","road_name":"明珠大道","step_distance":"92","polyline":"106.66966,26.408697;106.668738,26.408697"},{"instruction":"沿明珠大道向东行驶887米左转","orientation":"东","road_name":"明珠大道","step_distance":"887","polyline":"106.668738,26.408697;106.668705,26.408648;106.668727,26.408568;106.669676,26.408584;106.670599,26.40859;106.671393,26.408584;106.67149,26.408584;106.671725,26.40859;106.671822,26.408595;106.672123,26.4086;106.6723,26.408606;106.673319,26.408622;106.674585,26.408632;106.674864,26.408632;106.675084,26.408638;106.67533,26.408643;106.675711,26.408643;106.677063,26.408659;106.677245,26.408654;106.677541,26.408648"},{"instruction":"沿清溪路途径花溪桥、花溪大道南段向北行驶4.6千米向右前方行驶进入辅路","orientation":"北","road_name":"清溪路","step_distance":"4647","polyline":"106.677664,26.408766;106.677685,26.409115;106.677701,26.409373;106.677728,26.409802;106.67775,26.410172;106.677744,26.410617;106.67768,26.411009;106.677664,26.411132;106.677562,26.411738;106.677256,26.413117;106.677245,26.413181;106.677068,26.41426;106.677042,26.414646;106.676934,26.415472;106.676891,26.416201;106.676875,26.416711;106.676908,26.417548;106.676929,26.417875;106.677111,26.419173;106.677245,26.419887;106.677567,26.421695;106.677653,26.422118;106.677852,26.423079;106.678104,26.424259;106.678147,26.424398;106.678152,26.424436;106.678157,26.424473;106.678222,26.424828;106.678249,26.425037;106.678281,26.425509;106.678281,26.425681;106.678275,26.425976;106.678216,26.426501;106.678088,26.427279;106.678071,26.427365;106.678013,26.427681;106.677921,26.428207;106.677895,26.428443;106.677868,26.42884;106.677884,26.429495;106.677889,26.429918;106.677889,26.430315;106.677889,26.430358;106.677895,26.430466;106.677895,26.430541;106.6779,26.43068;106.677932,26.431673;106.677938,26.431742;106.67797,26.432359;106.67797,26.432429;106.677996,26.432831;106.677996,26.432896;106.678002,26.432965;106.678002,26.433105;106.678007,26.433368;106.677996,26.433545;106.677975,26.433738;106.677911,26.434081;106.677857,26.434258;106.677755,26.43451;106.67761,26.434838;106.677396,26.435428;106.677245,26.43583;106.677149,26.436077;106.677063,26.436323;106.677047,26.436404;106.677015,26.43664;106.67701,26.436747;106.67701,26.436801;106.67701,26.436935;106.677026,26.43774;106.677026,26.437783;106.677031,26.438196;106.677042,26.438727;106.677047,26.43929;106.677052,26.439569;106.677052,26.439601;106.677052,26.439692;106.677085,26.440352;106.677101,26.441259;106.677085,26.441763;106.676956,26.442991;106.676908,26.443426;106.676832,26.444091;106.676613,26.445996;106.676419,26.44739;106.676355,26.44776;106.676285,26.448173;106.676231,26.448447;106.67591,26.449955;106.675803,26.450571"},{"instruction":"沿花溪大道南段辅路向北行驶179米左转调头","orientation":"北","road_name":"花溪大道南段辅路","step_distance":"179","polyline":"106.675803,26.450571;106.675813,26.45114;106.675685,26.452181"},{"instruction":"沿花溪大道南段辅路途径花溪大道南段向南行驶268米到达目的地","orientation":"南","road_name":"花溪大道南段辅路","step_distance":"268","polyline":"106.675346,26.452138;106.67539,26.451773;106.675421,26.451558;106.675561,26.450791;106.675636,26.450566;106.675717,26.45041;106.675825,26.449761"}]},{"distance":"6248","restriction":"0","steps":[{"instruction":"向西南行驶77米右转","orientation":"西南","step_distance":"77","polyline":"106.67032,26.409636;106.6701,26.409609;106.670014,26.409582;106.669666,26.409308"},{"instruction":"向北行驶180米右转","orientation":"北","step_distance":"180","polyline":"106.669666,26.409308;106.669585,26.409351;106.669306,26.409598;106.669247,26.409657;106.669188,26.409743;106.669167,26.409775;106.669172,26.409871;106.669279,26.409973;106.669542,26.410199;106.669574,26.41029;106.66958,26.410617"},{"instruction":"向东行驶34米进入环岛","orientation":"东","step_distance":"34","polyline":"106.66958,26.410617;106.669923,26.410612"},{"instruction":"向东行驶81米离开环岛","orientation":"东","step_distance":"81","polyline":"106.669923,26.410612;106.670138,26.410526;106.670293,26.410505;106.670454,26.410526;106.670524,26.410542;106.670685,26.410633"},{"instruction":"向东行驶105米左转","orientation":"东","step_distance":"105","polyline":"106.670685,26.410633;106.671028,26.410628;106.671232,26.410628;106.671661,26.410639;106.671758,26.410639"},{"instruction":"沿田园路向北行驶994米靠左沿主路行驶","orientation":"北","road_name":"田园路","step_distance":"994","polyline":"106.671854,26.410639;106.671854,26.411132;106.671854,26.411186;106.671854,26.411266;106.671854,26.411685;106.671843,26.412736;106.671827,26.413112;106.671827,26.413181;106.671811,26.414453;106.67179,26.41544;106.671758,26.415735;106.671688,26.416116;106.671608,26.416432;106.671484,26.416818;106.671296,26.41728;106.671275,26.417333;106.671033,26.417784;106.670717,26.418256;106.670524,26.418503;106.669901,26.41919"},{"instruction":"沿田园路向北行驶1.2千米右转","orientation":"北","road_name":"田园路","step_distance":"1174","polyline":"106.669901,26.41919;106.669537,26.419463;106.669236,26.419715;106.669113,26.419828;106.668973,26.42001;106.668856,26.420268;106.668823,26.420407;106.668818,26.420525;106.668834,26.420745;106.668904,26.421303;106.668904,26.421491;106.668807,26.422118;106.668716,26.422773;106.668657,26.423213;106.668528,26.424473;106.668539,26.425665;106.668539,26.425809;106.668576,26.426753;106.668598,26.427805;106.668598,26.427853;106.668625,26.428481;106.66863,26.428534;106.668652,26.42942"},{"instruction":"沿贵筑路向东行驶908米左转","orientation":"东","road_name":"贵筑路","step_distance":"908","polyline":"106.668652,26.42942;106.669553,26.429398;106.669982,26.429387;106.670036,26.429387;106.670256,26.429387;106.670706,26.429409;106.67105,26.429441;106.671715,26.429521;106.672053,26.429564;106.672273,26.429591;106.672713,26.42965;106.672857,26.429666;106.673297,26.42972;106.673909,26.429806;106.674515,26.429891;106.674815,26.429929;106.675019,26.429956;106.675894,26.430069;106.67592,26.430074;106.676258,26.430122;106.676596,26.430165;106.676891,26.430203;106.677213,26.430246;106.677245,26.430251;106.677503,26.430283;106.677766,26.430315"},{"instruction":"沿清溪路途径花溪桥、花溪大道南段向北行驶2.2千米向右前方行驶进入辅路","orientation":"北","road_name":"清溪路","step_distance":"2248","polyline":"106.677889,26.430358;106.677895,26.430466;106.677895,26.430541;106.6779,26.43068;106.677932,26.431673;106.677938,26.431742;106.67797,26.432359;106.67797,26.432429;106.677996,26.432831;106.677996,26.432896;106.678002,26.432965;106.678002,26.433105;106.678007,26.433368;106.677996,26.433545;106.677975,26.433738;106.677911,26.434081;106.677857,26.434258;106.677755,26.43451;106.67761,26.434838;106.677396,26.435428;106.677245,26.43583;106.677149,26.436077;106.677063,26.436323;106.677047,26.436404;106.677015,26.43664;106.67701,26.436747;106.67701,26.436801;106.67701,26.436935;106.677026,26.43774;106.677026,26.437783;106.677031,26.438196;106.677042,26.438727;106.677047,26.43929;106.677052,26.439569;106.677052,26.439601;106.677052,26.439692;106.677085,26.440352;106.677101,26.441259;106.677085,26.441763;106.676956,26.442991;106.676908,26.443426;106.676832,26.444091;106.676613,26.445996;106.676419,26.44739;106.676355,26.44776;106.676285,26.448173;106.676231,26.448447;106.67591,26.449955;106.675803,26.450571"},{"instruction":"沿花溪大道南段辅路向北行驶179米左转调头","orientation":"北","road_name":"花溪大道南段辅路","step_distance":"179","polyline":"106.675803,26.450571;106.675813,26.45114;106.675685,26.452181"},{"instruction":"沿花溪大道南段辅路途径花溪大道南段向南行驶268米到达目的地","orientation":"南","road_name":"花溪大道南段辅路","step_distance":"268","polyline":"106.675346,26.452138;106.67539,26.451773;106.675421,26.451558;106.675561,26.450791;106.675636,26.450566;106.675717,26.45041;106.675825,26.449761"}]}]}}
//                """;

        return jsonStr;
    }


    /**
     * 公共交通的数据获取
     *
     * @param depLongitude
     * @param depLatitude
     * @param destLongitude
     * @param destLatitude
     * @param depCity
     * @param destCity
     * @return
     */
    public ResponseResult getDirectionTransit(String depLongitude, String depLatitude, String destLongitude, String destLatitude, String depCity, String destCity, String strategy) {
        String only = depLongitude + "-" +  depLatitude + "-" + destLongitude + "-" + destLatitude;
        // 数据写入redis中
        String jsonStr = redisCacheUtils.queryWithLogicalExpire(RedisPrefixUtils.TRANSIT_PREFIX,
                depLatitude + "-" + depLongitude + "-" + destLatitude + "-" + destLongitude + "-" + strategy,
                String.class, 120L, TimeUnit.MINUTES,
                "path:transit" + only, 5L, TimeUnit.MINUTES,
                10L, TimeUnit.MINUTES,
                (String s) -> {
//                    ResponseResult walkByStr = getWalkByStr(depLongitude, depLatitude, destLongitude, destLatitude, getWalkListStr(depLongitude, depLatitude, destLongitude, destLatitude));
                    return getTransitListStr(depLongitude, depLatitude, destLongitude, destLatitude, strategy, depCity, destCity);
                });
        return getTransitByStr(depLongitude, depLatitude, destLongitude, destLatitude, jsonStr);
    }

    private ResponseResult getTransitByStr(String depLongitude, String depLatitude, String destLongitude, String destLatitude, String jsonStr) {
        if(StringUtils.isEmpty(jsonStr)) {
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_ERROR);
        }
        // System.out.println(jsonStr);
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);

        // 判断请求状态是否正常
        if (!jsonObject.has("status") || !jsonObject.getString("status").equals("1")) {
            // 调用方需要自己去做检查
            return ResponseResult.fail("Invalid response status");
        }

        // 判断是否有 "route" 数据
        if (!jsonObject.has("route")) {
            return ResponseResult.fail("No route data found");
        }

        JSONObject route = jsonObject.getJSONObject("route");

        // 提取起点、终点及总距离
        String origin = route.getString("origin");
        String destination = route.getString("destination");
        Double distance = Double.parseDouble(route.getString("distance"));

        Double taxiFee = 0D;
        if(route.getJSONObject("cost").has("taxi_fee")) {
            taxiFee = route.getJSONObject("cost").getDouble("taxi_fee");
        }


        // 解析transits
        JSONArray transitsArray = route.getJSONArray("transits");
        List<DirectionTransitVO.Transit> transits = new ArrayList<>();

        for (int i = 0; i < transitsArray.size(); i++) {
            JSONObject transitObject = transitsArray.getJSONObject(i);

            // 解析每个Transit对象
            DirectionTransitVO.Transit transit = new DirectionTransitVO.Transit();
            transit.setDistance(transitObject.getInt("distance"));
            transit.setWalkDistance(transitObject.getString("walking_distance"));

            // 获取费用
            transit.setDuration(transitObject.getJSONObject("cost").getDouble("duration"));
            String fee = transitObject.getJSONObject("cost").getString("transit_fee");
            transit.setTransitFee(StringUtils.isBlank(fee) ? 0 : Double.parseDouble(fee));

            // 解析segments
            JSONArray segmentsArray = transitObject.getJSONArray("segments");
            List<DirectionSegment> segments = new ArrayList<>();
            for (int j = 0; j < segmentsArray.size(); j++) {
                segments.add(parseSegment(segmentsArray.getJSONObject(j)));
            }
            transit.setSegment(segments);

            // 将解析后的Transit对象添加到transits列表中
            transits.add(transit);
        }

        // 构建最终的VO对象
        DirectionTransitVO directionTransitVO = new DirectionTransitVO();
        directionTransitVO.setDepLongitude(depLongitude);
        directionTransitVO.setDepLatitude(depLatitude);

        directionTransitVO.setDestLongitude(destLongitude);
        directionTransitVO.setDestLatitude(destLatitude);
        directionTransitVO.setDistance(distance);
        directionTransitVO.setTransits(transits);
        directionTransitVO.setTaxiFee(taxiFee);


        return ResponseResult.success(directionTransitVO);
    }

    private String getTransitListStr(String depLongitude, String depLatitude, String destLongitude, String destLatitude, String strategy, String depCity,String destCity) {
        // https://restapi.amap.com/v5/direction/transit/integrated?origin=116.466485,39.995197&destination=116.46424,40.020642&key=<用户的key>&city1=010&city2=010
        // 拼接地址
        StringBuilder str = new StringBuilder();
        str.append(AmapConfigConstants.PUBLIC_TRANSPORTATION_HTTP);
        str.append("origin=");
        str.append(depLongitude + "," + depLatitude);
        str.append("&destination=");
        str.append(destLongitude + "," + destLatitude);
        str.append("&show_fields=polyline,cost");
        str.append("&key=");
        str.append(key);
        str.append("&city1=" + depCity);
        str.append("&city2=" + destCity);
        if (!StringUtils.isEmpty(strategy)) {
            str.append("&strategy=" + strategy.trim());
        }

        // 发送远程调用
        // 发送请求
        ResponseEntity<String> entity = restTemplate.getForEntity(str.toString(), String.class);
        System.out.println(entity.getBody());
        String jsonStr = entity.getBody();


        // 虚拟数据
//        String jsonStr = """
//                {"status":"1","info":"OK","infocode":"10000","route":{"origin":"106.672319,26.444301","destination":"106.670149,26.556007","distance":"11836","transits":[{"distance":"13667","walking_distance":"1359","nightflag":"0","segments":[{"walking":{"destination":"106.664932,26.447878","distance":"1008","origin":"106.672234,26.444401","steps":[{"instruction":"步行76米左转","road":"","distance":"76","polyline":{"polyline":"106.672234,26.444401;106.672203,26.444723;106.672173,26.445095"}},{"instruction":"沿践行路步行72米向右前方行走","road":"践行路","distance":"72","polyline":{"polyline":"106.672173,26.445095;106.671455,26.445156"}},{"instruction":"步行58米左转","road":"","distance":"58","polyline":{"polyline":"106.671448,26.445156;106.671249,26.445309;106.670998,26.445477"}},{"instruction":"步行101米靠左","road":"","distance":"101","polyline":{"polyline":"106.670998,26.445477;106.670822,26.445395;106.670723,26.445360;106.670586,26.445295;106.670464,26.445255;106.670326,26.445234;106.670189,26.445217;106.670044,26.445217"}},{"instruction":"步行267米向右前方行走","road":"","distance":"267","polyline":{"polyline":"106.670036,26.445213;106.669968,26.445196;106.669510,26.445213;106.668526,26.445274;106.668480,26.445274;106.668091,26.445330;106.667603,26.445602;106.667488,26.445677"}},{"instruction":"步行323米右转","road":"","distance":"323","polyline":{"polyline":"106.667480,26.445677;106.667244,26.446159;106.667198,26.446293;106.667168,26.446388;106.666992,26.446680;106.666924,26.446819;106.666862,26.446928;106.666763,26.447044;106.666695,26.447096;106.666512,26.447166;106.666443,26.447191;106.666321,26.447214;106.666260,26.447218;106.666092,26.447191;106.665962,26.447157;106.665848,26.447113;106.665756,26.447083;106.665672,26.447044;106.665611,26.447023;106.665306,26.446918"}},{"instruction":"沿甲秀南路步行111米到达贵州大学西校区","road":"甲秀南路","distance":"111","polyline":{"polyline":"106.665298,26.446915;106.665283,26.446962;106.664940,26.447838;106.664932,26.447878"}}]},"bus":{"buslines":[{"departure_stop":{"name":"贵州大学西校区","id":"900000219032003","location":"106.664936,26.447851"},"arrival_stop":{"name":"甲秀南路(北)","id":"900000219032008","location":"106.667923,26.557144"},"name":"223路大站快车(花溪平桥--金阳客站)","id":"900000219032","type":"普通公交线路","distance":"12308","polyline":{"polyline":"106.664936,26.447851;106.664886,26.447980;106.664866,26.448031;106.664445,26.449113;106.663903,26.450505;106.663791,26.450791;106.663743,26.451247;106.663679,26.451467;106.663505,26.451979;106.663464,26.452100;106.663314,26.452561;106.663158,26.453157;106.663141,26.453247;106.663133,26.453288;106.663103,26.453448;106.663103,26.453448;106.663103,26.453447;106.663089,26.453522;106.663067,26.453635;106.663041,26.453747;106.662987,26.454455;106.662965,26.454761;106.662858,26.455078;106.662837,26.455136;106.662858,26.455576;106.662906,26.455936;106.663148,26.457255;106.663260,26.457931;106.663330,26.458704;106.663330,26.459337;106.663314,26.459637;106.663260,26.460147;106.662617,26.464358;106.662552,26.464814;106.662504,26.465366;106.662483,26.465828;106.662483,26.466064;106.662504,26.466708;106.662729,26.468864;106.662778,26.469663;106.662810,26.470865;106.662788,26.471777;106.662745,26.472769;106.662574,26.474497;106.662643,26.474743;106.662611,26.475098;106.662552,26.475639;106.662472,26.476653;106.662477,26.477050;106.662477,26.477056;106.662472,26.477184;106.662474,26.477290;106.662474,26.477291;106.662474,26.477291;106.662474,26.477290;106.662483,26.477662;106.662477,26.479480;106.662440,26.479840;106.662493,26.480510;106.662579,26.481175;106.662767,26.482361;106.663218,26.485054;106.663491,26.486706;106.663711,26.488036;106.663727,26.488128;106.663920,26.489276;106.664119,26.490477;106.664205,26.491019;106.664285,26.491395;106.664408,26.491845;106.664913,26.493288;106.665004,26.493535;106.665390,26.494688;106.665508,26.495091;106.665712,26.495804;106.665787,26.496126;106.665862,26.496410;106.666077,26.497397;106.666152,26.497708;106.666259,26.498266;106.666404,26.499023;106.666436,26.499184;106.667000,26.501936;106.667305,26.503470;106.667648,26.505262;106.667670,26.505465;106.667761,26.506233;106.667804,26.506748;106.667820,26.507182;106.667836,26.508105;106.667820,26.508646;106.667777,26.509339;106.667713,26.510009;106.667606,26.511109;106.667552,26.511908;106.667541,26.512557;106.667547,26.513003;106.667590,26.513823;106.667708,26.514816;106.667804,26.515449;106.667906,26.515953;106.668239,26.517407;106.668276,26.517578;106.668370,26.518005;106.668370,26.518006;106.668370,26.518006;106.668382,26.518062;106.668764,26.519810;106.668904,26.520534;106.668990,26.520995;106.669086,26.521591;106.669285,26.522970;106.669381,26.523774;106.669488,26.525040;106.669537,26.525893;106.669703,26.528897;106.669730,26.529493;106.669748,26.529941;106.669754,26.530086;106.669754,26.530086;106.669773,26.530560;106.669778,26.531842;106.669778,26.531982;106.669746,26.532926;106.669708,26.533548;106.669623,26.534551;106.669585,26.534863;106.669338,26.536681;106.668936,26.539175;106.668915,26.539342;106.668662,26.540930;106.668620,26.541176;106.668582,26.541477;106.668523,26.541874;106.668351,26.542968;106.668239,26.543698;106.668088,26.544594;106.667793,26.546455;106.666892,26.552184;106.666774,26.552983;106.666720,26.553558;106.666715,26.553879;106.666720,26.553955;106.666742,26.554330;106.666763,26.554512;106.666849,26.554936;106.666967,26.555360;106.667080,26.555655;106.667203,26.555934;106.667413,26.556326;106.667531,26.556524;106.667815,26.556969;106.667923,26.557144"},"bus_time_tips":"下一班车05:00发出","bustimetag":"2","start_time":"","end_time":"","via_num":"4","via_stops":[{"name":"养牛村","id":"900000219032004","location":"106.663103,26.453448"},{"name":"竹林村","id":"900000219032005","location":"106.662474,26.477291"},{"name":"上坝村","id":"900000219032006","location":"106.668370,26.518006"},{"name":"下坝隧道","id":"900000219032007","location":"106.669754,26.530086"}]},{"departure_stop":{"name":"贵州大学西校区","id":"520100010354077","location":"106.664926,26.447879"},"arrival_stop":{"name":"甲秀南路(北)","id":"520100010354099","location":"106.668358,26.557058"},"name":"210路(青岩--花果园中央公园)","id":"520100010354","type":"普通公交线路","distance":"12333","polyline":{"polyline":"106.664926,26.447879;106.664886,26.447980;106.663791,26.450791;106.663743,26.451247;106.663679,26.451467;106.663539,26.451878;106.663464,26.452100;106.663314,26.452561;106.663158,26.453157;106.663151,26.453193;106.663111,26.453403;106.663111,26.453403;106.663089,26.453522;106.663041,26.453747;106.662978,26.454586;106.662965,26.454761;106.662896,26.455078;106.662853,26.455292;106.662858,26.455576;106.662878,26.455729;106.662906,26.455936;106.663095,26.456965;106.663148,26.457255;106.663260,26.457931;106.663279,26.458137;106.663303,26.458437;106.663303,26.458437;106.663330,26.458704;106.663330,26.459337;106.663314,26.459637;106.663260,26.460147;106.662617,26.464358;106.662552,26.464814;106.662504,26.465366;106.662483,26.465828;106.662483,26.466064;106.662504,26.466708;106.662729,26.468864;106.662778,26.469663;106.662802,26.470558;106.662802,26.470558;106.662810,26.470865;106.662788,26.471777;106.662745,26.472769;106.662590,26.474309;106.662627,26.474550;106.662638,26.474754;106.662611,26.475098;106.662552,26.475639;106.662472,26.476653;106.662477,26.477050;106.662477,26.477056;106.662472,26.477184;106.662474,26.477275;106.662474,26.477291;106.662474,26.477291;106.662481,26.477575;106.662483,26.477662;106.662476,26.479503;106.662445,26.479877;106.662493,26.480510;106.662579,26.481175;106.662767,26.482361;106.663218,26.485054;106.663491,26.486706;106.663711,26.488036;106.663727,26.488128;106.663920,26.489276;106.663960,26.489517;106.663960,26.489518;106.663960,26.489518;106.664119,26.490477;106.664205,26.491019;106.664285,26.491395;106.664408,26.491845;106.664913,26.493288;106.665004,26.493535;106.665390,26.494688;106.665508,26.495091;106.665712,26.495804;106.665787,26.496126;106.665862,26.496410;106.666077,26.497397;106.666152,26.497708;106.666259,26.498266;106.666404,26.499023;106.666436,26.499184;106.666531,26.499648;106.666531,26.499648;106.667000,26.501936;106.667305,26.503470;106.667648,26.505262;106.667670,26.505465;106.667761,26.506233;106.667804,26.506748;106.667820,26.507182;106.667836,26.508105;106.667820,26.508646;106.667777,26.509339;106.667713,26.510009;106.667606,26.511109;106.667552,26.511908;106.667541,26.512557;106.667547,26.513003;106.667590,26.513823;106.667708,26.514816;106.667804,26.515449;106.667906,26.515953;106.668239,26.517407;106.668276,26.517578;106.668370,26.518006;106.668370,26.518006;106.668764,26.519810;106.668904,26.520534;106.668990,26.520995;106.669086,26.521591;106.669285,26.522970;106.669381,26.523774;106.669487,26.525040;106.669487,26.525040;106.669537,26.525893;106.669703,26.528897;106.669730,26.529493;106.669773,26.530560;106.669778,26.531842;106.669778,26.531982;106.669746,26.532926;106.669708,26.533548;106.669623,26.534551;106.669585,26.534863;106.669338,26.536681;106.668936,26.539175;106.668915,26.539342;106.668662,26.540930;106.668620,26.541176;106.668582,26.541477;106.668523,26.541874;106.668351,26.542968;106.668239,26.543698;106.668088,26.544594;106.667793,26.546455;106.666892,26.552184;106.666774,26.552983;106.666720,26.553558;106.666715,26.553879;106.666720,26.553955;106.666742,26.554330;106.666763,26.554512;106.666849,26.554936;106.666967,26.555360;106.667080,26.555655;106.667203,26.555934;106.667413,26.556326;106.667531,26.556524;106.667815,26.556969;106.668358,26.557058"},"bus_time_tips":"下一班车07:30发出","bustimetag":"2","start_time":"0730","end_time":"2000","via_num":"8","via_stops":[{"name":"养牛村","id":"520100010354080","location":"106.663111,26.453403"},{"name":"中铁城","id":"520100010354086","location":"106.663303,26.458437"},{"name":"大职路立交","id":"520100010354079","location":"106.662802,26.470558"},{"name":"竹林村","id":"520100010354101","location":"106.662474,26.477291"},{"name":"电线厂宿舍区","id":"520100010354078","location":"106.663960,26.489518"},{"name":"甲秀南路(中)","id":"520100010354087","location":"106.666531,26.499648"},{"name":"上坝村","id":"520100010354100","location":"106.668370,26.518006"},{"name":"下坝村","id":"520100010354083","location":"106.669487,26.525040"}]},{"departure_stop":{"name":"贵州大学西校区","id":"520100010379063","location":"106.664936,26.447851"},"arrival_stop":{"name":"甲秀南路(北)","id":"520100010379094","location":"106.667894,26.557096"},"name":"223路(花溪平桥--金阳客站)","id":"520100010379","type":"普通公交线路","distance":"12297","polyline":{"polyline":"106.664936,26.447851;106.664927,26.447875;106.664886,26.447980;106.664828,26.448130;106.663904,26.450507;106.663824,26.451017;106.663679,26.451467;106.663464,26.452100;106.663314,26.452561;106.663296,26.452631;106.663294,26.452636;106.663158,26.453157;106.663103,26.453448;106.663102,26.453450;106.663102,26.453450;106.663089,26.453522;106.663067,26.453635;106.663041,26.453747;106.663032,26.453860;106.662987,26.454455;106.662965,26.454761;106.662858,26.455078;106.662837,26.455136;106.662845,26.455308;106.662858,26.455576;106.662906,26.455936;106.663047,26.456707;106.663148,26.457255;106.663260,26.457931;106.663276,26.458103;106.663303,26.458437;106.663303,26.458437;106.663330,26.458704;106.663330,26.459337;106.663314,26.459637;106.663260,26.460147;106.662617,26.464358;106.662552,26.464814;106.662504,26.465366;106.662483,26.465828;106.662483,26.466064;106.662504,26.466708;106.662729,26.468864;106.662778,26.469663;106.662798,26.470411;106.662798,26.470411;106.662810,26.470865;106.662788,26.471777;106.662745,26.472769;106.662590,26.474309;106.662627,26.474550;106.662638,26.474754;106.662611,26.475098;106.662552,26.475639;106.662472,26.476653;106.662477,26.477050;106.662477,26.477056;106.662472,26.477184;106.662474,26.477291;106.662474,26.477291;106.662474,26.477293;106.662474,26.477296;106.662481,26.477564;106.662483,26.477662;106.662482,26.477822;106.662481,26.477985;106.662477,26.479480;106.662440,26.479840;106.662493,26.480510;106.662579,26.481175;106.662767,26.482361;106.663218,26.485054;106.663491,26.486706;106.663711,26.488036;106.663727,26.488128;106.663920,26.489276;106.663955,26.489485;106.663956,26.489485;106.663956,26.489485;106.664119,26.490477;106.664205,26.491019;106.664285,26.491395;106.664408,26.491845;106.664913,26.493288;106.665004,26.493535;106.665390,26.494688;106.665508,26.495091;106.665712,26.495804;106.665787,26.496126;106.665862,26.496410;106.666077,26.497397;106.666152,26.497708;106.666259,26.498266;106.666404,26.499023;106.666436,26.499184;106.666531,26.499648;106.666531,26.499648;106.666531,26.499647;106.667000,26.501936;106.667305,26.503470;106.667648,26.505262;106.667670,26.505465;106.667761,26.506233;106.667804,26.506748;106.667820,26.507182;106.667835,26.508030;106.667835,26.508031;106.667835,26.508031;106.667835,26.508074;106.667836,26.508105;106.667820,26.508646;106.667777,26.509339;106.667713,26.510009;106.667606,26.511109;106.667552,26.511908;106.667541,26.512557;106.667547,26.513003;106.667590,26.513823;106.667708,26.514816;106.667804,26.515449;106.667906,26.515953;106.668239,26.517407;106.668276,26.517578;106.668764,26.519810;106.668904,26.520534;106.668990,26.520995;106.669086,26.521591;106.669285,26.522970;106.669381,26.523774;106.669487,26.525040;106.669487,26.525040;106.669488,26.525040;106.669492,26.525115;106.669537,26.525893;106.669703,26.528897;106.669730,26.529493;106.669755,26.530101;106.669755,26.530101;106.669773,26.530560;106.669778,26.531842;106.669778,26.531982;106.669746,26.532926;106.669708,26.533548;106.669623,26.534551;106.669585,26.534863;106.669338,26.536681;106.668936,26.539175;106.668915,26.539342;106.668662,26.540930;106.668620,26.541176;106.668582,26.541477;106.668523,26.541874;106.668351,26.542968;106.668239,26.543698;106.668088,26.544594;106.667793,26.546455;106.666892,26.552184;106.666774,26.552983;106.666720,26.553558;106.666715,26.553879;106.666720,26.553955;106.666742,26.554330;106.666763,26.554512;106.666849,26.554936;106.666967,26.555360;106.667080,26.555655;106.667203,26.555934;106.667413,26.556326;106.667531,26.556524;106.667815,26.556969;106.667894,26.557096"},"bus_time_tips":"下一班车06:30发出","bustimetag":"2","start_time":"0630","end_time":"2200","via_num":"9","via_stops":[{"name":"养牛村","id":"520100010379044","location":"106.663102,26.453450"},{"name":"中铁城","id":"520100010379045","location":"106.663303,26.458437"},{"name":"大职路立交","id":"520100010379070","location":"106.662798,26.470411"},{"name":"竹林村","id":"520100010379092","location":"106.662474,26.477291"},{"name":"电线厂宿舍区","id":"520100010379093","location":"106.663956,26.489485"},{"name":"甲秀南路(中)","id":"520100010379095","location":"106.666531,26.499648"},{"name":"谢家坡","id":"520100010379089","location":"106.667835,26.508031"},{"name":"下坝村","id":"520100010379103","location":"106.669487,26.525040"},{"name":"下坝隧道","id":"520100010379090","location":"106.669755,26.530101"}]}]}},{"walking":{"destination":"106.670319,26.555990","distance":"351","origin":"106.667892,26.557093","steps":[{"instruction":"沿甲秀南路步行16米左转","road":"甲秀南路","distance":"16","polyline":{"polyline":"106.667892,26.557093;106.667816,26.556971"}},{"instruction":"沿都会大道步行242米往前走","road":"都会大道","distance":"242","polyline":{"polyline":"106.667816,26.556967;106.667892,26.556961;106.668182,26.557009;106.668327,26.557032;106.668526,26.557066;106.668839,26.557079;106.668900,26.557079;106.668991,26.557070;106.669067,26.557056;106.669662,26.556923;106.669792,26.556889;106.669937,26.556858;106.670090,26.556835;106.670090,26.556814;106.670074,26.556684"}},{"instruction":"步行19米左转","road":"","distance":"19","polyline":{"polyline":"106.670067,26.556679;106.670067,26.556511"}},{"instruction":"步行74米","road":"","distance":"74","polyline":{"polyline":"106.670067,26.556505;106.670197,26.556480;106.670326,26.556376;106.670341,26.556171;106.670319,26.555990"}}]}}]},{"distance":"13817","walking_distance":"1454","nightflag":"0","segments":[{"walking":{"destination":"106.664932,26.447878","distance":"1008","origin":"106.672234,26.444401","steps":[{"instruction":"步行76米左转","road":"","distance":"76","polyline":{"polyline":"106.672234,26.444401;106.672203,26.444723;106.672173,26.445095"}},{"instruction":"沿践行路步行72米向右前方行走","road":"践行路","distance":"72","polyline":{"polyline":"106.672173,26.445095;106.671455,26.445156"}},{"instruction":"步行58米左转","road":"","distance":"58","polyline":{"polyline":"106.671448,26.445156;106.671249,26.445309;106.670998,26.445477"}},{"instruction":"步行101米靠左","road":"","distance":"101","polyline":{"polyline":"106.670998,26.445477;106.670822,26.445395;106.670723,26.445360;106.670586,26.445295;106.670464,26.445255;106.670326,26.445234;106.670189,26.445217;106.670044,26.445217"}},{"instruction":"步行267米向右前方行走","road":"","distance":"267","polyline":{"polyline":"106.670036,26.445213;106.669968,26.445196;106.669510,26.445213;106.668526,26.445274;106.668480,26.445274;106.668091,26.445330;106.667603,26.445602;106.667488,26.445677"}},{"instruction":"步行323米右转","road":"","distance":"323","polyline":{"polyline":"106.667480,26.445677;106.667244,26.446159;106.667198,26.446293;106.667168,26.446388;106.666992,26.446680;106.666924,26.446819;106.666862,26.446928;106.666763,26.447044;106.666695,26.447096;106.666512,26.447166;106.666443,26.447191;106.666321,26.447214;106.666260,26.447218;106.666092,26.447191;106.665962,26.447157;106.665848,26.447113;106.665756,26.447083;106.665672,26.447044;106.665611,26.447023;106.665306,26.446918"}},{"instruction":"沿甲秀南路步行111米到达贵州大学西校区","road":"甲秀南路","distance":"111","polyline":{"polyline":"106.665298,26.446915;106.665283,26.446962;106.664940,26.447838;106.664932,26.447878"}}]},"bus":{"buslines":[{"departure_stop":{"name":"贵州大学西校区","id":"520100010409062","location":"106.664838,26.448104"},"arrival_stop":{"name":"甲秀南路(北)","id":"520100010409073","location":"106.668366,26.557853"},"name":"255路(大学城--火车站)","id":"520100010409","type":"普通公交线路","distance":"12363","polyline":{"polyline":"106.664838,26.448104;106.664838,26.448103;106.663791,26.450791;106.663743,26.451247;106.663679,26.451467;106.663464,26.452100;106.663314,26.452561;106.663158,26.453157;106.663111,26.453403;106.663111,26.453403;106.663089,26.453522;106.663041,26.453747;106.662978,26.454586;106.662965,26.454761;106.662896,26.455078;106.662853,26.455292;106.662858,26.455576;106.662878,26.455729;106.662906,26.455936;106.663095,26.456965;106.663148,26.457255;106.663260,26.457931;106.663279,26.458137;106.663303,26.458437;106.663303,26.458437;106.663330,26.458704;106.663330,26.459337;106.663314,26.459637;106.663260,26.460147;106.662617,26.464358;106.662552,26.464814;106.662504,26.465366;106.662483,26.465828;106.662483,26.466064;106.662504,26.466708;106.662729,26.468864;106.662778,26.469663;106.662798,26.470411;106.662798,26.470411;106.662810,26.470865;106.662788,26.471777;106.662745,26.472769;106.662590,26.474309;106.662627,26.474550;106.662638,26.474754;106.662611,26.475098;106.662552,26.475639;106.662472,26.476653;106.662477,26.477050;106.662477,26.477056;106.662472,26.477184;106.662474,26.477291;106.662474,26.477293;106.662474,26.477296;106.662481,26.477564;106.662483,26.477662;106.662482,26.477761;106.662482,26.477761;106.662482,26.477822;106.662481,26.477985;106.662477,26.479480;106.662440,26.479840;106.662493,26.480510;106.662579,26.481175;106.662767,26.482361;106.663218,26.485054;106.663491,26.486706;106.663711,26.488036;106.663727,26.488128;106.663920,26.489276;106.663955,26.489485;106.663956,26.489485;106.663956,26.489485;106.664119,26.490477;106.664205,26.491019;106.664285,26.491395;106.664408,26.491845;106.664913,26.493288;106.665004,26.493535;106.665390,26.494688;106.665508,26.495091;106.665712,26.495804;106.665787,26.496126;106.665862,26.496410;106.666077,26.497397;106.666152,26.497708;106.666259,26.498266;106.666404,26.499023;106.666436,26.499184;106.666531,26.499648;106.666531,26.499648;106.667000,26.501936;106.667305,26.503470;106.667648,26.505262;106.667670,26.505465;106.667761,26.506233;106.667804,26.506748;106.667820,26.507182;106.667834,26.508021;106.667834,26.508021;106.667836,26.508105;106.667820,26.508646;106.667777,26.509339;106.667713,26.510009;106.667606,26.511109;106.667552,26.511908;106.667541,26.512557;106.667547,26.513003;106.667590,26.513823;106.667708,26.514816;106.667804,26.515449;106.667906,26.515953;106.668239,26.517407;106.668276,26.517578;106.668370,26.518006;106.668370,26.518006;106.668764,26.519810;106.668904,26.520534;106.668990,26.520995;106.669086,26.521591;106.669285,26.522970;106.669381,26.523774;106.669487,26.525040;106.669487,26.525040;106.669537,26.525893;106.669703,26.528897;106.669730,26.529493;106.669754,26.530086;106.669754,26.530086;106.669773,26.530560;106.669778,26.531842;106.669778,26.531982;106.669746,26.532926;106.669708,26.533548;106.669623,26.534551;106.669585,26.534863;106.669338,26.536681;106.668936,26.539175;106.668915,26.539342;106.668662,26.540930;106.668620,26.541176;106.668582,26.541477;106.668523,26.541874;106.668351,26.542968;106.668239,26.543698;106.668088,26.544594;106.667793,26.546455;106.666892,26.552184;106.666774,26.552983;106.666720,26.553558;106.666715,26.553879;106.666720,26.553955;106.666742,26.554330;106.666763,26.554512;106.666849,26.554936;106.666967,26.555360;106.667080,26.555655;106.667203,26.555934;106.667413,26.556326;106.667531,26.556524;106.667815,26.556969;106.667923,26.557144;106.668169,26.557538;106.668255,26.557677;106.668366,26.557853"},"bus_time_tips":"下一班车06:30发出","bustimetag":"2","start_time":"","end_time":"","via_num":"10","via_stops":[{"name":"养牛村","id":"520100010409063","location":"106.663111,26.453403"},{"name":"中铁城","id":"520100010409064","location":"106.663303,26.458437"},{"name":"大职路立交","id":"520100010409065","location":"106.662798,26.470411"},{"name":"竹林村","id":"520100010409066","location":"106.662482,26.477761"},{"name":"电线厂宿舍区","id":"520100010409067","location":"106.663956,26.489485"},{"name":"甲秀南路(中)","id":"520100010409068","location":"106.666531,26.499648"},{"name":"谢家坡","id":"520100010409069","location":"106.667834,26.508021"},{"name":"上坝村","id":"520100010409070","location":"106.668370,26.518006"},{"name":"下坝村","id":"520100010409071","location":"106.669487,26.525040"},{"name":"下坝隧道","id":"520100010409072","location":"106.669754,26.530086"}]}]}},{"walking":{"destination":"106.670319,26.555990","distance":"446","origin":"106.668365,26.557852","steps":[{"instruction":"沿甲秀南路步行111米左转","road":"甲秀南路","distance":"111","polyline":{"polyline":"106.668365,26.557852;106.668259,26.557678;106.668175,26.557543;106.667816,26.556971"}},{"instruction":"沿都会大道步行242米往前走","road":"都会大道","distance":"242","polyline":{"polyline":"106.667816,26.556967;106.667892,26.556961;106.668182,26.557009;106.668327,26.557032;106.668526,26.557066;106.668839,26.557079;106.668900,26.557079;106.668991,26.557070;106.669067,26.557056;106.669662,26.556923;106.669792,26.556889;106.669937,26.556858;106.670090,26.556835;106.670090,26.556814;106.670074,26.556684"}},{"instruction":"步行19米左转","road":"","distance":"19","polyline":{"polyline":"106.670067,26.556679;106.670067,26.556511"}},{"instruction":"步行74米","road":"","distance":"74","polyline":{"polyline":"106.670067,26.556505;106.670197,26.556480;106.670326,26.556376;106.670341,26.556171;106.670319,26.555990"}}]}}]},{"distance":"15218","walking_distance":"1962","nightflag":"0","segments":[{"walking":{"destination":"106.675888,26.449770","distance":"1137","origin":"106.672234,26.444401","steps":[{"instruction":"步行76米右转","road":"","distance":"76","polyline":{"polyline":"106.672234,26.444401;106.672203,26.444723;106.672173,26.445095"}},{"instruction":"沿践行路步行17米右转","road":"践行路","distance":"17","polyline":{"polyline":"106.672173,26.445095;106.672340,26.445087"}},{"instruction":"沿践行路步行262米左转","road":"践行路","distance":"262","polyline":{"polyline":"106.672340,26.445082;106.672348,26.445030;106.672356,26.445017;106.672371,26.445009;106.672394,26.445000;106.672409,26.445000;106.672966,26.445065;106.673981,26.444986;106.674080,26.444986;106.674179,26.444979;106.674377,26.444962;106.674904,26.444962"}},{"instruction":"步行109米右转","road":"","distance":"109","polyline":{"polyline":"106.674904,26.444962;106.674858,26.445156;106.674774,26.445295;106.674690,26.445377;106.674591,26.445534;106.674530,26.445625;106.674469,26.445799;106.674438,26.445843"}},{"instruction":"步行23米往前走","road":"","distance":"23","polyline":{"polyline":"106.674438,26.445843;106.674660,26.445890"}},{"instruction":"步行183米左转","road":"","distance":"183","polyline":{"polyline":"106.674660,26.445890;106.674995,26.445921;106.675735,26.445990;106.676079,26.445986;106.676285,26.445963;106.676498,26.445980"}},{"instruction":"沿花溪大道南段步行315米左转","road":"花溪大道南段","distance":"315","polyline":{"polyline":"106.676498,26.445980;106.676392,26.446728;106.676369,26.446884;106.676231,26.447691;106.676132,26.448164;106.676086,26.448317;106.676079,26.448359;106.676071,26.448433;106.675995,26.448811"}},{"instruction":"步行19米右转","road":"","distance":"19","polyline":{"polyline":"106.675995,26.448811;106.675804,26.448772"}},{"instruction":"步行133米到达贵州大学","road":"","distance":"133","polyline":{"polyline":"106.675804,26.448767;106.675728,26.449175;106.675995,26.449232;106.675949,26.449484;106.675888,26.449770"}}]},"bus":{"buslines":[{"departure_stop":{"name":"贵州大学","id":"900000132330007","location":"106.675885,26.449770","entrance":{"name":"C口","location":"106.675812,26.448769"}},"arrival_stop":{"name":"五里冲","id":"900000132330015","location":"106.674639,26.558665","exit":{"name":"C口","location":"106.673775,26.557623"}},"name":"轨道交通3号线(桐木岭(省委党校)--洛湾)","id":"900000132330","type":"地铁线路","distance":"13256","polyline":{"polyline":"106.675885,26.449770;106.675712,26.450602;106.675549,26.451643;106.675477,26.452389;106.675412,26.453930;106.675104,26.459714;106.675083,26.460181;106.675083,26.460181;106.675054,26.460713;106.674986,26.462559;106.674949,26.463064;106.674904,26.463327;106.674825,26.463656;106.674717,26.463970;106.674544,26.464413;106.674019,26.465707;106.673893,26.466027;106.673767,26.466384;106.673712,26.466567;106.673661,26.466771;106.673599,26.467114;106.673530,26.467777;106.672740,26.477068;106.672691,26.477729;106.672691,26.477729;106.672644,26.478394;106.672665,26.478814;106.672720,26.479422;106.672787,26.480102;106.672907,26.481065;106.673044,26.482359;106.673232,26.483432;106.673361,26.484037;106.673613,26.485101;106.674026,26.486776;106.674745,26.489681;106.675391,26.492336;106.675842,26.494348;106.675906,26.494703;106.675950,26.495015;106.676212,26.497377;106.676491,26.500536;106.676662,26.502154;106.676724,26.502722;106.676724,26.502722;106.676864,26.503884;106.676983,26.505253;106.677109,26.506828;106.677214,26.508549;106.677297,26.511215;106.677416,26.519109;106.677417,26.519684;106.677417,26.519684;106.677425,26.520277;106.677481,26.521501;106.677516,26.522015;106.677583,26.522478;106.677720,26.522958;106.677918,26.523534;106.678302,26.524280;106.679455,26.526128;106.681856,26.529785;106.682159,26.530236;106.682317,26.530479;106.682317,26.530479;106.682749,26.531117;106.683249,26.531990;106.683511,26.532475;106.683707,26.532895;106.683920,26.533477;106.684183,26.534234;106.684845,26.536204;106.685473,26.538064;106.686146,26.540137;106.686200,26.540368;106.686245,26.540589;106.686294,26.540842;106.686339,26.541181;106.686382,26.541941;106.686440,26.542530;106.686440,26.542530;106.686508,26.543212;106.686717,26.544725;106.686919,26.546158;106.686978,26.546864;106.686967,26.547641;106.686785,26.548543;106.686506,26.549258;106.686291,26.549705;106.686012,26.550137;106.685551,26.550741;106.684852,26.551569;106.684359,26.552146;106.684184,26.552328;106.684018,26.552468;106.683852,26.552563;106.683688,26.552641;106.683408,26.552732;106.683193,26.552790;106.682421,26.552943;106.682421,26.552943;106.681662,26.553092;106.680532,26.553227;106.680280,26.553261;106.680138,26.553286;106.679944,26.553319;106.678937,26.553491;106.675513,26.554256;106.675183,26.554345;106.674821,26.554448;106.674464,26.554580;106.674129,26.554789;106.673917,26.555014;106.673700,26.555304;106.673577,26.555564;106.673528,26.555904;106.673550,26.556298;106.673625,26.556605;106.673729,26.556946;106.673936,26.557328;106.674639,26.558665"},"bus_time_tips":"不在运营时间内","bustimetag":"2","start_time":"","end_time":"","via_num":"7","via_stops":[{"name":"孔学堂","id":"900000132330008","location":"106.675083,26.460181"},{"name":"十里河滩","id":"900000132330009","location":"106.672691,26.477729"},{"name":"中曹司","id":"900000132330010","location":"106.676724,26.502722"},{"name":"甘荫塘","id":"900000132330011","location":"106.677417,26.519684"},{"name":"四方河(省中医院)","id":"900000132330012","location":"106.682317,26.530479"},{"name":"皂角井","id":"900000132330013","location":"106.686440,26.542530"},{"name":"太慈桥","id":"900000132330014","location":"106.682421,26.552943"}]}]}},{"walking":{"destination":"106.670319,26.555990","distance":"825","origin":"106.674629,26.558668","steps":[{"instruction":"步行165米向右前方行走","road":"","distance":"165","polyline":{"polyline":"106.674629,26.558668;106.674622,26.558651;106.674126,26.557709;106.673874,26.557795;106.673782,26.557621"}},{"instruction":"步行156米往前走","road":"","distance":"156","polyline":{"polyline":"106.673782,26.557617;106.673752,26.557634;106.673477,26.557009;106.673401,26.556940;106.673286,26.556896;106.673172,26.556896;106.672974,26.556919;106.672729,26.556950"}},{"instruction":"步行32米往前走","road":"","distance":"32","polyline":{"polyline":"106.672729,26.556950;106.672424,26.557045"}},{"instruction":"步行24米左转","road":"","distance":"24","polyline":{"polyline":"106.672424,26.557045;106.672195,26.557110"}},{"instruction":"步行12米往前走","road":"","distance":"12","polyline":{"polyline":"106.672188,26.557110;106.672180,26.557066;106.672249,26.557049"}},{"instruction":"步行79米左转","road":"","distance":"79","polyline":{"polyline":"106.672249,26.557045;106.672394,26.557022;106.672333,26.556627;106.672531,26.556614"}},{"instruction":"步行4米左转","road":"","distance":"4","polyline":{"polyline":"106.672531,26.556610;106.672531,26.556650"}},{"instruction":"沿都会大道步行44米往前走","road":"都会大道","distance":"44","polyline":{"polyline":"106.672531,26.556650;106.672089,26.556671"}},{"instruction":"步行27米往前走","road":"","distance":"27","polyline":{"polyline":"106.672089,26.556671;106.671822,26.556698"}},{"instruction":"沿都会大道步行189米往前走","road":"都会大道","distance":"189","polyline":{"polyline":"106.671822,26.556698;106.671562,26.556719;106.671486,26.556728;106.670280,26.556801;106.670090,26.556831;106.670090,26.556814;106.670074,26.556684"}},{"instruction":"步行19米左转","road":"","distance":"19","polyline":{"polyline":"106.670067,26.556679;106.670067,26.556511"}},{"instruction":"步行74米","road":"","distance":"74","polyline":{"polyline":"106.670067,26.556505;106.670197,26.556480;106.670326,26.556376;106.670341,26.556171;106.670319,26.555990"}}]}}]},{"distance":"13663","walking_distance":"1359","nightflag":"0","segments":[{"walking":{"destination":"106.664932,26.447878","distance":"1008","origin":"106.672234,26.444401","steps":[{"instruction":"步行76米左转","road":"","distance":"76","polyline":{"polyline":"106.672234,26.444401;106.672203,26.444723;106.672173,26.445095"}},{"instruction":"沿践行路步行72米向右前方行走","road":"践行路","distance":"72","polyline":{"polyline":"106.672173,26.445095;106.671455,26.445156"}},{"instruction":"步行58米左转","road":"","distance":"58","polyline":{"polyline":"106.671448,26.445156;106.671249,26.445309;106.670998,26.445477"}},{"instruction":"步行101米靠左","road":"","distance":"101","polyline":{"polyline":"106.670998,26.445477;106.670822,26.445395;106.670723,26.445360;106.670586,26.445295;106.670464,26.445255;106.670326,26.445234;106.670189,26.445217;106.670044,26.445217"}},{"instruction":"步行267米向右前方行走","road":"","distance":"267","polyline":{"polyline":"106.670036,26.445213;106.669968,26.445196;106.669510,26.445213;106.668526,26.445274;106.668480,26.445274;106.668091,26.445330;106.667603,26.445602;106.667488,26.445677"}},{"instruction":"步行323米右转","road":"","distance":"323","polyline":{"polyline":"106.667480,26.445677;106.667244,26.446159;106.667198,26.446293;106.667168,26.446388;106.666992,26.446680;106.666924,26.446819;106.666862,26.446928;106.666763,26.447044;106.666695,26.447096;106.666512,26.447166;106.666443,26.447191;106.666321,26.447214;106.666260,26.447218;106.666092,26.447191;106.665962,26.447157;106.665848,26.447113;106.665756,26.447083;106.665672,26.447044;106.665611,26.447023;106.665306,26.446918"}},{"instruction":"沿甲秀南路步行111米到达贵州大学西校区","road":"甲秀南路","distance":"111","polyline":{"polyline":"106.665298,26.446915;106.665283,26.446962;106.664940,26.447838;106.664932,26.447878"}}]},"bus":{"buslines":[{"departure_stop":{"name":"贵州大学西校区","id":"900000118414014","location":"106.664936,26.447851"},"arrival_stop":{"name":"甲秀南路(北)","id":"900000118414019","location":"106.667894,26.557096"},"name":"806路(大学城--轨道八鸽岩站)","id":"900000118414","type":"普通公交线路","distance":"12304","polyline":{"polyline":"106.664936,26.447851;106.664886,26.447980;106.663791,26.450791;106.663250,26.452213;106.663100,26.452739;106.662933,26.453490;106.662880,26.453881;106.662826,26.454493;106.662837,26.455078;106.662837,26.455136;106.662858,26.455576;106.662906,26.455936;106.663148,26.457255;106.663260,26.457931;106.663330,26.458704;106.663330,26.459337;106.663314,26.459637;106.663260,26.460147;106.662617,26.464358;106.662552,26.464814;106.662504,26.465366;106.662483,26.465828;106.662483,26.466064;106.662504,26.466708;106.662729,26.468864;106.662778,26.469663;106.662810,26.470865;106.662788,26.471777;106.662745,26.472769;106.662574,26.474497;106.662499,26.474936;106.662429,26.475596;106.662348,26.476760;106.662343,26.477050;106.662332,26.477651;106.662354,26.478702;106.662402,26.479539;106.662440,26.479840;106.662493,26.480510;106.662579,26.481175;106.662767,26.482361;106.663218,26.485054;106.663491,26.486706;106.663711,26.488036;106.663727,26.488128;106.663920,26.489276;106.664119,26.490477;106.664205,26.491019;106.664285,26.491395;106.664408,26.491845;106.664913,26.493288;106.665004,26.493535;106.665390,26.494688;106.665508,26.495091;106.665712,26.495804;106.665787,26.496126;106.665862,26.496410;106.666077,26.497397;106.666152,26.497708;106.666259,26.498266;106.666404,26.499023;106.666436,26.499184;106.666531,26.499648;106.666531,26.499648;106.667000,26.501936;106.667305,26.503470;106.667648,26.505262;106.667670,26.505465;106.667761,26.506233;106.667804,26.506748;106.667820,26.507182;106.667836,26.508105;106.667820,26.508646;106.667777,26.509339;106.667713,26.510009;106.667606,26.511109;106.667552,26.511908;106.667541,26.512557;106.667547,26.513003;106.667590,26.513823;106.667708,26.514816;106.667804,26.515449;106.667906,26.515953;106.668239,26.517407;106.668276,26.517578;106.668764,26.519810;106.668904,26.520534;106.668990,26.520995;106.669086,26.521591;106.669285,26.522970;106.669381,26.523774;106.669488,26.525040;106.669537,26.525893;106.669703,26.528897;106.669730,26.529493;106.669773,26.530560;106.669778,26.531842;106.669778,26.531982;106.669746,26.532926;106.669708,26.533548;106.669623,26.534551;106.669585,26.534863;106.669338,26.536681;106.668936,26.539175;106.668915,26.539342;106.668662,26.540930;106.668620,26.541176;106.668582,26.541477;106.668523,26.541874;106.668351,26.542968;106.668239,26.543698;106.668088,26.544594;106.667793,26.546455;106.666892,26.552184;106.666774,26.552983;106.666720,26.553558;106.666715,26.553879;106.666720,26.553955;106.666742,26.554330;106.666763,26.554512;106.666849,26.554936;106.666967,26.555360;106.667080,26.555655;106.667203,26.555934;106.667413,26.556326;106.667531,26.556524;106.667815,26.556969;106.667894,26.557096"},"bus_time_tips":"下一班车07:00发出","bustimetag":"2","start_time":"0700","end_time":"1900","via_num":"1","via_stops":[{"name":"甲秀南路(中)","id":"900000118414017","location":"106.666531,26.499648"}]}]}},{"walking":{"destination":"106.670319,26.555990","distance":"351","origin":"106.667892,26.557093","steps":[{"instruction":"沿甲秀南路步行16米左转","road":"甲秀南路","distance":"16","polyline":{"polyline":"106.667892,26.557093;106.667816,26.556971"}},{"instruction":"沿都会大道步行242米往前走","road":"都会大道","distance":"242","polyline":{"polyline":"106.667816,26.556967;106.667892,26.556961;106.668182,26.557009;106.668327,26.557032;106.668526,26.557066;106.668839,26.557079;106.668900,26.557079;106.668991,26.557070;106.669067,26.557056;106.669662,26.556923;106.669792,26.556889;106.669937,26.556858;106.670090,26.556835;106.670090,26.556814;106.670074,26.556684"}},{"instruction":"步行19米左转","road":"","distance":"19","polyline":{"polyline":"106.670067,26.556679;106.670067,26.556511"}},{"instruction":"步行74米","road":"","distance":"74","polyline":{"polyline":"106.670067,26.556505;106.670197,26.556480;106.670326,26.556376;106.670341,26.556171;106.670319,26.555990"}}]}}]}]},"count":"4"}
//                """;
        return jsonStr;
    }

    /**
     * 解析segment数据
     *
     * @param jobject
     * @return
     */
    private DirectionSegment parseSegment(JSONObject jobject) {
        DirectionSegment segment = new DirectionSegment();

        // 解析步行数据
        if (jobject.has("walking")) {
            walkParse(jobject, segment);
        }

        // 解析公交、铁路、
        if (jobject.has("bus")) {
            busParse(jobject, segment);
        }

        // 解析火车动车数据
        if (jobject.has("railway")) {
            railwayParse(jobject, segment);
        }

        // 解析出租车数据
        if (jobject.has("taxi")) {
            taxiParse(jobject, segment);
        }
        return segment;
    }

    private void taxiParse(JSONObject jobject, DirectionSegment segment) {
        JSONObject taxiObject = jobject.getJSONObject("taxi");
        // 解析打车信息
        String startPoint = taxiObject.getString("startpoint");
        String startName = taxiObject.getString("startname"); // 如果没有提供，可以默认为空
        String endPoint = taxiObject.getString("endpoint");
        String endName = taxiObject.getString("endname");
        String polyline = taxiObject.getJSONObject("polyline").getString("polyline");

        String distance = taxiObject.getString("distance");
        String price = taxiObject.getString("price");
        String driveTime = taxiObject.getString("drivetime");

        // 创建并返回打车对象
        segment.setTaxi(new DirectionSegment.Taxi(startPoint, startName, endPoint, endName, polyline, distance, driveTime, price));
    }

    private void railwayParse(JSONObject jobject, DirectionSegment segment) {
        // 解析Railway部分
        JSONObject railwayObject = jobject.getJSONObject("railway");
        // 解析铁路信息
        String departureStopName = railwayObject.getJSONObject("departure_stop").getString("name");
        String departureStopLocation = railwayObject.getJSONObject("departure_stop").getString("location");
        String departureStopTime = railwayObject.getJSONObject("departure_stop").getString("time");

        String arrivalStopName = railwayObject.getJSONObject("arrival_stop").getString("name");
        String arrivalStopLocation = railwayObject.getJSONObject("arrival_stop").getString("location");
        String arrivalStopTime = railwayObject.getJSONObject("arrival_stop").getString("time");

        String railName = railwayObject.getString("name");
        String distance = railwayObject.getString("distance");
        String time = railwayObject.getString("time");

        // 解析票价信息
        JSONArray spacesArray = railwayObject.getJSONArray("spaces");
        List<DirectionSegment.Railway.Space> spaces = new ArrayList<>();
        for (int i = 0; i < spacesArray.size(); i++) {
            JSONObject spaceObject = spacesArray.getJSONObject(i);
            String code = spaceObject.getString("code");
            String cost = spaceObject.getString("cost");
            spaces.add(new DirectionSegment.Railway.Space(code, cost));
        }

        // 创建并返回铁路对象
        segment.setRailway(new DirectionSegment.Railway(departureStopName, departureStopLocation, departureStopTime,
                arrivalStopName, arrivalStopLocation, arrivalStopTime,
                railName, distance, time, spaces));

    }

    private void busParse(JSONObject jobject, DirectionSegment segment) {
        JSONObject busObject = jobject.getJSONObject("bus");
        JSONArray buslinesArray = busObject.getJSONArray("buslines");
        List<DirectionSegment.BusLine> busLines = new ArrayList<>();

        for (int i = 0; i < buslinesArray.size(); i++) {
            JSONObject buslineObject = buslinesArray.getJSONObject(i);

            // 起始站和到达站
            JSONObject departureStop = buslineObject.getJSONObject("departure_stop");
            JSONObject arrivalStop = buslineObject.getJSONObject("arrival_stop");

            // 途经站点
            JSONArray viaStopsArray = buslineObject.getJSONArray("via_stops");
            List<DirectionSegment.BusLine.ViaStop> viaStops = new ArrayList<>();
            for (int j = 0; j < viaStopsArray.size(); j++) {
                JSONObject viaStopObject = viaStopsArray.getJSONObject(j);
                DirectionSegment.BusLine.ViaStop viaStop = new DirectionSegment.BusLine.ViaStop(
                        viaStopObject.getString("name"),
                        viaStopObject.getString("location")
                );
                viaStops.add(viaStop);
            }

            // 创建BusLine对象
            DirectionSegment.BusLine busLine = new DirectionSegment.BusLine(
                    departureStop.getString("name"),
                    departureStop.getString("location"),
                    arrivalStop.getString("name"),
                    arrivalStop.getString("location"),
                    buslineObject.getString("name"),
                    buslineObject.getString("type"),
                    buslineObject.getString("distance"),
                    buslineObject.getJSONObject("polyline").getString("polyline"),
                    buslineObject.getString("start_time"),
                    buslineObject.getString("end_time"),
                    viaStops
            );

            busLines.add(busLine);
        }

        segment.setBusLines(busLines);  // 将公交数据添加到segment中
    }

    private void walkParse(JSONObject jobject, DirectionSegment segment) {
        JSONObject walkingObject = jobject.getJSONObject("walking");

        // 步行目的地、距离、起点
        String destination = walkingObject.getString("destination");
        String distance = walkingObject.getString("distance");
        String origin = walkingObject.getString("origin");

        // 解析步行步骤
        JSONArray stepsArray = walkingObject.getJSONArray("steps");
        List<DirectionSegment.Walk.Step> walkSteps = new ArrayList<>();

        for (int i = 0; i < stepsArray.size(); i++) {
            JSONObject stepObject = stepsArray.getJSONObject(i);
            DirectionSegment.Walk.Step walkStep = new DirectionSegment.Walk.Step(
                    stepObject.getString("instruction"), // 步骤指示
                    stepObject.optString("road", ""),    // 路名（如果没有则为空字符串）
                    stepObject.optString("code", ""),    // 步骤的code（若没有可以为空字符串）
                    stepObject.getJSONObject("polyline").getString("polyline") // 路径坐标
            );
            walkSteps.add(walkStep);
        }

        // 将解析出的数据填充到Walk对象
        DirectionSegment.Walk walk = new DirectionSegment.Walk(destination, distance, origin, walkSteps);
        segment.setWalk(walk);  // 将步行数据添加到segment中
    }

    /**
     * 骑行的出行方式的接口
     *
     * @param depLongitude
     * @param depLatitude
     * @param destLongitude
     * @param destLatitude
     * @return
     */
    public ResponseResult getBicycle(String depLongitude, String depLatitude, String destLongitude, String destLatitude) {

        // 数据写入redis中
        String jsonStr = redisCacheUtils.queryWithLogicalExpire(RedisPrefixUtils.BICYCLE_PREFIX,
                depLatitude + "-" + depLongitude + "-" + destLatitude + "-" + destLongitude,
                String.class, 120L, TimeUnit.MINUTES,
                "path:bicycle", 5L, TimeUnit.MINUTES,
                10L, TimeUnit.MINUTES,
                (String s) -> {
//                    ResponseResult walkByStr = getWalkByStr(depLongitude, depLatitude, destLongitude, destLatitude, getWalkListStr(depLongitude, depLatitude, destLongitude, destLatitude));
                    return getBicycleListStr(depLongitude, depLatitude, destLongitude, destLatitude);
                });
        return getBicycleByStr(depLongitude, depLatitude, destLongitude, destLatitude, jsonStr);
    }

    private ResponseResult getBicycleByStr(String depLongitude, String depLatitude, String destLongitude, String destLatitude, String jsonStr) {
        if(StringUtils.isEmpty(jsonStr)) {
            return ResponseResult.fail(CommonStatusEnum.GET_DATA_ERROR);
        }

        JSONObject jsonObject = JSONObject.fromObject(jsonStr);

        // 判断请求状态是否正常
        if (!jsonObject.has("status") || !jsonObject.getString("status").equals("1")) {
            // 调用方需要自己去做检查
            return ResponseResult.fail("Invalid response status");
        }

        // 判断是否有 "route" 数据
        if (!jsonObject.has("route")) {
            return ResponseResult.fail("No route data found");
        }

        JSONObject route = jsonObject.getJSONObject("route");

        // 提取起点、终点
        String origin = route.getString("origin");
        String destination = route.getString("destination");

        // 解析 paths
        JSONArray pathsArray = route.getJSONArray("paths");
        List<DirectionBicycleVO.Path> paths = new ArrayList<>();
        for (int i = 0; i < pathsArray.size(); i++) {
            JSONObject pathObj = pathsArray.getJSONObject(i);
            String distance = pathObj.getString("distance");
            String duration = pathObj.getString("duration");

            // 解析 steps
            JSONArray stepsArray = pathObj.getJSONArray("steps");
            List<DirectionBicycleVO.Step> steps = new ArrayList<>();
            for (int j = 0; j < stepsArray.size(); j++) {
                JSONObject stepObj = stepsArray.getJSONObject(j);
                String instruction = stepObj.getString("instruction");
                String orientation = stepObj.getString("orientation");
                String roadName = stepObj.getString("road_name");
                String stepDistance = stepObj.getString("step_distance");
                String polyline = stepObj.getString("polyline");

                DirectionBicycleVO.Step step = new DirectionBicycleVO.Step(instruction, orientation, roadName, stepDistance, polyline);
                steps.add(step);
            }

            DirectionBicycleVO.Path path = new DirectionBicycleVO.Path(distance, duration, steps);
            paths.add(path);
        }

        DirectionBicycleVO directionBicycleVO = new DirectionBicycleVO(origin, destination, paths);
        List<DirectionBicycleVO> resultList = new ArrayList<>();
        resultList.add(directionBicycleVO);

        return ResponseResult.success(resultList);
    }

    private String getBicycleListStr(String depLongitude, String depLatitude, String destLongitude, String destLatitude) {
        // 拼接URL
        StringBuilder str = new StringBuilder();
        str.append(AmapConfigConstants.BICYCLE_HTTP);
        str.append("origin=");
        str.append(depLongitude + "," + depLatitude);
        str.append("&destination=");
        str.append(destLongitude + "," + destLatitude);
        str.append("&show_fields=polyline");
        str.append("&key=");
        str.append(key);
        str.append("&alternative_route=" + 3);
        ResponseEntity<String> entity = restTemplate.getForEntity(str.toString(), String.class);
        System.out.println(entity.getBody());
        String jsonStr = entity.getBody();
        return jsonStr;
    }

    /**
     * 调用python提供的接口获取 推荐的策略。
     * 暂时还待完善
     * @param userId
     * @return
     */
    public Integer getRecommendWay(Long userId, String depLongitude, String depLatitude, String destLongitude, String destLatitude) {
        // 拼接URL
        StringBuilder str = new StringBuilder();
        str.append(AmapConfigConstants.RECOMMEND_HTTP);
        str.append("o_lng=");
        str.append(depLongitude + "&" + "o_lat=" + depLatitude);
        str.append("&d_lng=");
        str.append(destLongitude + "&d_lat=" + destLatitude);
        str.append("&user_id=");
        str.append(userId);
        ResponseEntity<String> entity = restTemplate.getForEntity(str.toString(), String.class);
        System.out.println(entity.getBody());
        String jsonStr = entity.getBody();
        System.out.println(jsonStr);

        Integer code = null;
        try {
            JSONObject jsonObject = JSONObject.fromObject(jsonStr);
            JSONObject recommended = jsonObject.getJSONObject("recommended");
            code = Integer.parseInt(recommended.getString("code"));
        } catch (Exception e) {
            log.info(e.getMessage());
            return -1;
        }

        return convert(code);
//        return 1;
//        return 2;
//        return 3;
//        return 4;
    }

    /**
     * 推荐code 转换
     * @param code
     * @return
     */
    public Integer convert(int code) {
        if(code == 4) {
            return RecommendUtil.Traffic.WALK.code;
        }
        if(code == 2 || code == 3) {
            return RecommendUtil.Traffic.DRIVE.code;
        }
        if(code == 5) {
            return RecommendUtil.Traffic.BICYCLE.code;
        }
        return RecommendUtil.Traffic.TRANSIT.code;
    }
}
